package seatiger.tomcat.catalina.connector;


import seatiger.tomcat.catalina.Connector;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 *  Response 对象，对 seatiger.tomcat.coyote.Response包装
 */
public class NewResponse implements HttpServletResponse {

    /**
     * 关联的输出缓冲区
     */
    protected OutputBuffer outputBuffer;

    /**
     * 关联的输出流
     */
    protected CoyoteOutputStream outputStream;

    /**
     * 关联的连接对象
     */
    protected Connector connector;

    protected seatiger.tomcat.coyote.Response coyoteResponse;

    /**
     */
    protected HttpResponseFacade facade = null;

    protected List<Cookie> cookies = new ArrayList<>();

    /**
     * Return the <code>ServletResponse</code> for which this object
     * is the facade.
     */
    public HttpServletResponse getResponse() {
        if (facade == null) {
            facade = new HttpResponseFacade(this);
        }
        return (facade);
    }

    public void setConnector(Connector connector) {
        this.connector = connector;
        //TODO 8 * 1024
        this.outputBuffer = new OutputBuffer(8 * 1024);
        this.outputStream = new CoyoteOutputStream(outputBuffer);
    }

    public void setCoyoteResponse(seatiger.tomcat.coyote.Response coyoteResponse) {
        this.coyoteResponse = coyoteResponse;
        outputBuffer.setResponse(coyoteResponse);
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (outputStream == null) {
            return new CoyoteOutputStream(outputBuffer);
        }
        return outputStream;
    }

    /**
     * 在报文头中添加Cookie
     * @param cookie
     */
    public void addCookieInternal(final Cookie cookie) {
        if (isCommitted()) {
            return;
        }
        cookies.add(cookie);
        StringBuilder builder = new StringBuilder();
        builder.append(cookie.getName()).append("=").append(cookie.getValue()).append(";Path=").append(cookie.getPath());
        addHeader("Set-Cookie",builder.toString());
    }

    @Override
    public void addCookie(Cookie cookie) {

    }

    @Override
    public boolean containsHeader(String name) {
        return false;
    }

    @Override
    public String encodeURL(String url) {
        return null;
    }

    @Override
    public String encodeRedirectURL(String url) {
        return null;
    }

    @Override
    public String encodeUrl(String url) {
        return null;
    }

    @Override
    public String encodeRedirectUrl(String url) {
        return null;
    }

    @Override
    public void sendError(int sc, String msg) throws IOException {

    }

    @Override
    public void sendError(int sc) throws IOException {

    }

    @Override
    public void sendRedirect(String location) throws IOException {

    }

    @Override
    public void setDateHeader(String name, long date) {

    }

    @Override
    public void addDateHeader(String name, long date) {

    }

    @Override
    public void setHeader(String name, String value) {

    }

    @Override
    public void addHeader(String name, String value) {
        if (isCommitted()) {
            return;
        }
        coyoteResponse.addHeader(name,value);
    }

    @Override
    public void setIntHeader(String name, int value) {

    }

    @Override
    public void addIntHeader(String name, int value) {

    }

    @Override
    public void setStatus(int sc) {

    }

    @Override
    public void setStatus(int sc, String sm) {

    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }



    @Override
    public PrintWriter getWriter() throws IOException {
        return null;
    }

    @Override
    public void setContentLength(int len) {

    }

    @Override
    public void setContentType(String type) {

    }

    @Override
    public void setBufferSize(int size) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale loc) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    /**
     * 完成流中数据的写出和关闭
     */
    public void finishResponse() throws IOException{
        outputBuffer.close();
        /*coyoteResponse.addHeader("server","my_tomcat");
        coyoteResponse.addHeader("Date",new Date().toString());
        outputStream.flush();*/
    }
}
